package com.jgy.service.impl;

import com.jgy.entity.SpaceUser;
import com.jgy.entity.vo.LoginInfoVO;
import com.jgy.entity.vo.UserInfo;
import com.jgy.service.AccountService;
import com.jgy.utils.MapperUtils;
import com.jgy.entity.Permission;
import com.jgy.utils.R;
import com.jgy.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Service;
import com.jgy.client.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 姜光远
 * @date 2023/3/24 16:23
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private OAuthLoginApi oAuthLoginApi;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    Map<String, String> params = new HashMap<>(16);


    @Override
    public R login(LoginInfoVO loginInfoVO) throws Exception {
        Map<String, String> params = new HashMap<>(16);

        params.put("client_id", "XcWebApp");
        params.put("client_secret", "XcWebApp");
        // 登录方式
        String loginType = loginInfoVO.getLoginType();
        if (StringUtils.isEmpty(loginType)) {
            throw new RuntimeException("登录方式不能为空");
        }

        // 密码方式登录
        if ("password".equals(loginType)) {
            String accountName = loginInfoVO.getAccountName();
            String password = loginInfoVO.getPassword();
            Map<String, String> map = new HashMap<>();
            map.put("username", accountName);
            map.put("password", password);
            map.put("authType", loginType);
            String s = MapperUtils.mapToJson(map);
            params.put("username",s);
            params.put("grant_type", loginType);
        }


        String jsonString = oAuthLoginApi.oauthToken(params);
        Map<String, Object> authInfo = MapperUtils.json2map(jsonString);
        String accessToken = (String) authInfo.get(OAuth2AccessToken.ACCESS_TOKEN);

        // save refresh token redis
        //String refreshToken = (String) authInfo.get(OAuth2AccessToken.REFRESH_TOKEN);
        //redisTemplate.opsForValue().set(
        //        String.format(CacheNameConstant.REFRESH_TOKEN_KEY, accessToken),
        //        refreshToken,
        //        SecurityConstant.ACCESS_TOKEN_VALIDITY,
        //        TimeUnit.SECONDS
        //);
        Map<String,Object> map = new HashMap<>();
        map.put("token",accessToken);
        Map map1 = (Map) authInfo.get("springsecurity");
        SpaceUser user = MapperUtils.json2pojo(map1.get("username").toString(), SpaceUser.class);
        List<Map> authorities = (List<Map>) map1.get("authorities");
        List<Permission> permissions = new ArrayList<>();
        authorities.forEach(v->{
            Permission permission = MapperUtils.obj2pojo(v, Permission.class);
            permissions.add(permission);
        });
        user.setPermissions(permissions);
        map.put("user",user);
        UserUtils.put(map);
        return R.ok(map);
    }
}
